package cs;
import java.io.*;
import java.net.*;

public class MyServer1 {
    public static void main(String[] args) {
        try {
            ServerSocket st=new ServerSocket(8888);
            System.out.println("服务器启动了");
            while(true){
                try {
                    Socket s = st.accept();//接收一个链接
                    System.out.println("ip:" + s.getInetAddress() + ",端口:" + s.getPort() + ",本地端口:" + s.getLocalPort());
                    InputStream in = s.getInputStream();
                    OutputStream out = s.getOutputStream();
                    //接收文本
                    BufferedReader br=new BufferedReader(new InputStreamReader(in));
                    PrintWriter pw=new PrintWriter(out,true);//自动刷新
                    String str=null;
                    while(true){
                        str=br.readLine();
                        System.out.println("客户端说:"+str);
                        pw.println("你刚才说的:["+str+"]朕知道了");
                    }
//                    while((str=br.readLine())!=null){
//                        System.out.println("客户端发来的信息:"+str);
//                    }
                }catch (Exception e){
                    System.out.println("流中断了");
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
